package com.guotd.jspwork2.controller;

import com.guotd.jspwork2.utils.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import static com.guotd.jspwork2.utils.JDBCUtil.getConnection;

@WebServlet("/getAllKinds")
public class getAllKindsServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Object[]> kindList = new ArrayList<>();
        /*try (Connection con = getConnection();
             Statement stmt = con.createStatement();
             ResultSet rs = stmt.executeQuery("SELECT * FROM kind")) {
            while (rs.next()) {
                String id = rs.getString("id");
                String kind = rs.getString("kind");
                String desc = rs.getString("desc");
                kindList.add(kind);
            }
        } catch (SQLException e) {
            throw new ServletException(e);
        }*/
        String sql="SELECT * FROM kind";
        String []args={};
        String []colNames ={"id","kind","desc"};
        kindList=JDBCUtil.query(sql,args,colNames);
        request.getSession().setAttribute("kindList", kindList);
        response.sendRedirect("kind_list.jsp");
    }
}
